<template>
  <div id="app">
    <br />
    
    <el-form class="login-container" label-position="left" label-width="0px">
      <h3 class="login_title" style="font-size:31px" >书店系统登录</h3>
      <el-form-item>
        
        <el-input
          type="text" 
          style="background-color:#ffffff94"
          v-model="loginForm.userName"
          class=“enterpriseName”
        
          auto-complete="off"
          placeholder="账号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          type="password"
          v-model="loginForm.password"
          auto-complete="off"
          placeholder="密码"
          id="password"
        >
          <template #suffix>
            <el-icon
              id="yanjing"
              @click="yanjing"
              :size="20"
              style="display: block"
              ><View
            /></el-icon>
            <el-icon
              id="hideyanjing"
              @click="hideyanjing"
              :size="20"
              style="display: none"
              ><Hide
            /></el-icon>
          </template>
        </el-input>
        <el-form-item>
          <el-row :span="42">
            <el-col :span="13">
              <el-input
                v-model="ruleForm.code"
                auto-complete="off"
                placeholder="请输入验证码"
                size="56px"
              ></el-input>
            </el-col>
            <el-col :span="1">
              <div class="login-code" @click="refreshCode">
                <!--验证码组件-->
                <sldentify :identifyCode="identifyCode"></sldentify>
              </div>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form-item>
      <el-form-item style="width: 100%">
        <el-button
          type="primary"
          style="width: 100%; background: #505458; border: none"
          @click="Login(), identify()"
          @keydown.enter="Login"
          >登录</el-button
        >
      </el-form-item>

      <el-form-item style="width: 20%">
        <a
          href="http://101.33.228.125/register"
          style="
            width: 100%;
            background: blanchedalmond;
            border: none;
            text-decoration: none;
          "
          >注册</a
        >
      </el-form-item>
    </el-form>
    <br />
    <!-- <router-link class="home" to="/home">Home</router-link>|
        <keep-alive include="News">
        <router-view></router-view>
        </keep-alive> -->
        <a href="https://beian.miit.gov.cn/" style="    width: 32%;
    height: 10%;
    margin-top: 90%;
    margin-left: 87%;">
          <p>粤ICP备2023012217号-1</p>
        </a>
    <buy-dialog ref="buyDialog"></buy-dialog>
  </div>
</template>
    
    <script>
//   import { info } from "console";
//   import { success } from "log-symbols";
//   import eventBus from "./eventBus";
import BuyDialog from "../components/qiantai/BuyDialog.vue";
import Sldentify from "../components/qiantai/Sldentify.vue";
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
       userName: "",
        password: "",
       
      },
  
      responseResult: [],
      value: "100",
      xianshi: false,
      ruleForm: {
        code: "",
      },
      identifyCode: "",
      identifyflag: false,
      successResponse: 0,
    };
  },
  components: { BuyDialog, Sldentify },
  methods: {
    Login() {
      this.$axios
        .post("/login", {
          userName: this.loginForm.userName,
          password: this.loginForm.password,
        })
        .then((resp) => {
          if (resp.data.code === 200 && this.identifyflag) {
            this.successResponse = 200;
            this.send();
            console.log("this.successResponse" + this.successResponse);
            var data = new FormData();
            data.append("userName", this.loginForm.userName);
            this.$axios.post("/getuserid", data).then((resp) => {
              sessionStorage.setItem("userId", resp.data);
            });
            sessionStorage.setItem("userName", this.loginForm.userName);
         
            //给vuex存储
            this.$store.commit('login', this.loginForm)
             //获取拦截的路径
            var path = this.$route.query.redirect
            //跳转到拦截的路径
              this.$router.replace({path: path === '/' || path === undefined ? '/index' : path})
            this.suceesslogin();
          } else if (resp.data.code === 201 && this.identifyflag) {
            this.successResponse = 201;
            this.send();
            console.log("this.successResponse" + this.successResponse);
            var data = new FormData();
            data.append("userName", this.loginForm.userName);
            this.$axios.post("/getuserid", data).then((resp) => {
              sessionStorage.setItem("userId", resp.data);
            });
            sessionStorage.setItem("userName", this.loginForm.userName);
        //给vuex存储
        this.$store.commit('login', this.loginForm)
             //获取拦截的路径
            var path = this.$route.query.redirect
            
            //跳转到拦截的路径
              this.$router.replace({path: path == '/index' || path === undefined ? '/usermanagement' : path})
            // this.suceesslogin();
          } else if (resp.data.code == 404) {
            this.$message.error("密码/用户名错误！");
          }
        });
    },
    send() {
      this.$bus.emit("success", this.successResponse);
    },
    on() {
      this.$bus.on("success", (val) => {
        console.log(val);
        this.$bus.data = val;
        sessionStorage.setItem("busdata", this.$bus.data);
      });
    },
    yanjing() {
      var password = document.getElementById("password");

      password.type = "text";
      var yanjing = document.getElementById("yanjing");
      yanjing.style.display = "none";
      var hideyanjing = document.getElementById("hideyanjing");
      hideyanjing.style.display = "block";

      console.log(25252525);
    },
    hideyanjing() {
      var password = document.getElementById("password");

      password.type = "password";
      var yanjing = document.getElementById("yanjing");
      yanjing.style.display = "block";
      var hideyanjing = document.getElementById("hideyanjing");
      hideyanjing.style.display = "none";

      console.log(25252525);
    },
    randomNum(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    },
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
    },
    makeCode(o, l) {
      for (let i = 0; i < l; i++) {
        this.identifyCode += this.randomNum(0, 10);
      }
    },
    identify() {
      console.log(99999999);
      if (this.identifyCode != this.ruleForm.code) {
        this.$message({
          message: "验证码失败",
          type: "error",
        });
        this.identifyflag = false;
      } else {
        this.identifyflag = true;
      }
    },
    suceesslogin() {
      this.$notify({
        title: "成功",
      
        message: "欢迎光临📚📚📚📚📚🌈",
        type: "success",
      });
    },
  },
  mounted: function () {
    this.on();
    this.refreshCode();
  },
};
</script>
<style scoped>
/* #a1 {
  background: url("../assets/eva.jpg");
  width: 100%;
  height: 100%;
  position: fixed;
  background-position: center;
  background-size: cover;
} */
::v-deep .enterpriseName .el-input__inner {
    width:100%;
    background-color: #e8e8e8;
    text-align: center;
    border-color: #c0c4cc;
    color:#000
}
.login-container {
  border-radius: 15px;
  background-clip: padding-box;
  margin: -100px 580px;
  width: 350px;
  padding: 35px 35px 15px 35px;
  background: #ffffff57;
  border: 1px solid #eaeaea;
  box-shadow: 0 0 25px #cac6c6;
  position: absolute;
  margin-top: 19%;
  margin-left: 40%;
}
.el-input__wrapper {
    display: inline-flex;
    flex-grow: 1;
    align-items: center;
    justify-content: center;
    padding: 1px 11px;
    background-color: #ffffff07;
    background-image: none;
    border-radius: var(--el-input-border-radius,var(--el-border-radius-base));
    transition: var(--el-transition-box-shadow);
    transform: translate3d(0,0,0);
    box-shadow: 0 0 0 1px var(--el-input-border-color,var(--el-border-color)) inset;
}
.el-input__inner {
    display: inline-flex;
    flex-grow: 1;
    align-items: center;
    justify-content: center;
    padding: 1px 11px;
    background-color: #ffffff07;
    background-image: none;
    border-radius: var(--el-input-border-radius,var(--el-border-radius-base));
    transition: var(--el-transition-box-shadow);
    transform: translate3d(0,0,0);
    box-shadow: 0 0 0 1px var(--el-input-border-color,var(--el-border-color)) inset;
}

.login_title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
#app {
  background-image: url("../assets/5.jpg");
  background-repeat: repeat;
  background-size: 1200rem 633rem;
  background-position: center;
  height: 100%;
  width: 100%;
  padding: 0%;
  background-size: cover;
  position: fixed;
}
div {
  margin: 0;
}
.el-form-item {
  background-color: unset !important;
}
el-switch el-tooltip is-checked {
  margin-top: -100px;
}
.el-input__wrapper{
  background-color: #ffffff2f;
}



</style>